#!/usr/bin/perl

require "accounts.pl";

&ReadParse();

my $td_width=100;
my $input_groupname='';
my $i;
my $glist_length=0;
my @line;
my @local_user_list;
my %local_user_disabled;
my %local_user_fullname;
my $error_msg = "";
my $local_members = "";
my $group_title;

@local_user_list = &accounts_user_list(\%local_user_fullname, \%local_user_fullname);

$error_msg = $text{"error_msg_no_groupname"};

if($in{"submitbutton"} eq "create")
{
	$input_groupname = '';
}
elsif($in{"submitbutton"} eq "modify")
{
	$input_groupname = $in{'groupname'};
}

if($in{"submitbutton"} eq "create")
{
	$group_title = $text{"create_title"};
	&header($group_title, "", "creategroup", 0, 1);
}
elsif($in{"submitbutton"} eq "modify")
{
	$group_title = $text{"modify_title"};
	&header($group_title, "", "modifygroup", 0, 1);
}

print "<form action='edit_group.cgi' method=post onsubmit='return on_submit(\"$error_msg\");'>\n";
print "<table border = 0 width=95% class=mode1>\n";

print "<tr><td width=$td_width valign=top>$text{'group_name'}</td><td><input name=groupname class=text type=text value=\"$input_groupname\"></td></tr>\n";

print "<tr><td width=$td_width valign=center>$text{'group_members'}</td><td>\n";

print "<table border=0 class=mode1>\n"; #table group members
print "<tr><td>$text{'edit_group_members_added'}</td><td></td><td>$text{'edit_group_members_toadd'}</td></tr>";
print "<tr><td>\n"; #td1  groupmembers
print "<select name=gmembers size=7 class=list2 multiple>\n";

if($in{'submitbutton'} eq "create")
{
	@line = ();
}
elsif($in{'submitbutton'} eq "modify")
{
	@line = split(",", $in{'memberselect'});
	@line = sort @line;
}
$i = 0;
while($i < @line)
{
	print "<option value=\"$line[$i]\">$line[$i]\n";
	$glist_length++;
	$i++;
}
print "</select>\n";

$local_members = join(" ", @line);
print "<input type=hidden name=entrygmembers value=\"$local_members\">\n";
print "<input type=hidden name=submitbutton value=\"$in{'submitbutton'}\">\n";

print "</td><td>\n";         #td2 button table

print "<table>\n";
print "<tr><td><input name=addall type=button class=sbutton value=\"$text{'edit_group_button_addall'}\" onclick='on_button_addall_click();'></td></tr>\n";
print "<tr><td><input name=removeall type=button class=sbutton value=\"$text{'edit_group_button_removeall'}\" onclick='on_button_removeall_click();'></td></tr>\n";
print "<tr><td><input name=all type=button class=sbutton value=\"$text{'edit_group_button_add'}\" onclick='on_button_add_click();'></td></tr>\n";
print "<tr><td><input name=remove type=button class=sbutton value=\"$text{'edit_group_button_remove'}\" onclick='on_button_remove_click();'></td></tr>\n";
print "</table>\n";

print "</td><td>\n";        #td3 available user

print "<select name=accountlist class=list2 size=7 multiple>";
$i = 0;
while($i < @local_user_list)
{
	print "<option value=\"$local_user_list[$i]\">$local_user_list[$i]\n";
	$i++;
}
print "</select>\n";

print "</td></tr>\n";
print "</table>\n";

print "</table>\n";

#print "<tr><td></td><td>";
print "<table border=0 cellspacing=0>";
print "<tr><td width=105>";
if($in{"submitbutton"} eq "create")
{
	$error_msg = $text{"error_msg_no_groupname"};
	print "<input name=submit1 type=submit class=button1 value=\"$text{'edit_group_submit_create'}\" onclick='return on_create_click(\"$error_msg\");'>\n";
}
if($in{"submitbutton"} eq "modify")
{
	$error_msg = $text{"error_msg_no_groupname"};
	print "<input name=submit1 type=submit class=button1 value=\"$text{'edit_group_submit_modify'}\" onclick='return on_modify_click(\"$error_msg\");'>\n";
}
print "</td><td>";
print "<input name=submitcancle type=submit class=button1 value=\"$text{'edit_group_submit_cancel'}\" onclick='on_cancel_click();'>\n";
print "</td></tr>";
print "</table>";
print "<input name=oldname type=hidden value=$input_groupname>\n";

print "</form>";

print <<EOF;
<script>
function on_button_addall_click()
{
	var i;
	var text1;

	for(i = document.forms[0].gmembers.options.length; i > 0; i--)
	{
		document.forms[0].gmembers.options[i - 1] = null;
	}

	for(i = 0; i < document.forms[0].accountlist.options.length; i++)
	{
		//alert("aa");
		text1 = document.forms[0].accountlist.options[i].text;
		document.forms[0].gmembers.options[i] = new Option(text1, text1);

	}

	if(document.forms[0].gmembers.options.length > 0)
	{
		document.forms[0].entrygmembers.value = document.forms[0].gmembers.options[0].text;
	}
	else
	{
		document.forms[0].entrygmembers.value = "";
	}

	for(i = 1; i < document.forms[0].gmembers.options.length; i++)
	{
		document.forms[0].entrygmembers.value += " " + document.forms[0].gmembers.options[i].text;
	}
}

function on_button_removeall_click()
{
	for(i = document.forms[0].gmembers.options.length; i > 0; i--)
	{
		document.forms[0].gmembers.options[i - 1] = null;
	}

	document.forms[0].entrygmembers.value = "";
}

function on_button_add_click()
{
	var j= document.forms[0].gmembers.options.length;
	var i;

	for(i = document.forms[0].accountlist.options.length; i > 0; i--)
	{
		if(document.forms[0].accountlist.options[i - 1].selected)
		{
			if(find_item_in_gmembers(document.forms[0].accountlist.options[i - 1].text) == 0)
			{
				document.forms[0].gmembers.options[j] = new Option(document.forms[0].accountlist.options[i - 1].text, document.forms[0].accountlist.options[i - 1].text);
				j = document.forms[0].gmembers.options.length;
			}
		}
	}

	if(document.forms[0].gmembers.options.length > 0)
	{
		document.forms[0].entrygmembers.value = document.forms[0].gmembers.options[0].text;
	}
	else
	{
		document.forms[0].entrygmembers.value = "";
	}

	for(i = 1; i < document.forms[0].gmembers.options.length; i++)
	{
		document.forms[0].entrygmembers.value += " " + document.forms[0].gmembers.options[i].text;
	}
}

function on_button_remove_click()
{
	for(i = document.forms[0].gmembers.options.length; i > 0; i--)
	{
		if(document.forms[0].gmembers.options[i - 1].selected)
		{
			document.forms[0].gmembers.options[i - 1] = null;
		}
	}

	if(document.forms[0].gmembers.options.length > 0)
	{
		document.forms[0].entrygmembers.value = document.forms[0].gmembers.options[0].text;
	}
	else
	{
		document.forms[0].entrygmembers.value = "";
	}

	for(i = 1; i < document.forms[0].gmembers.options.length; i++)
	{
		document.forms[0].entrygmembers.value += " " + document.forms[0].gmembers.options[i].text;
	}
}

function find_item_in_gmembers(text1)
{
	var rt = 0;
	var ii;
	for(ii = 0; ii < document.forms[0].gmembers.options.length; ii++)
	{
		if(document.forms[0].gmembers.options[ii].text == text1)
		{
			rt = 1;
		}
	}
	return rt;
}

function on_create_click(error_msg)
{
	document.forms[0].submitbutton.value = "create";
	if(document.forms[0].groupname.value == "")
	{
		alert(error_msg);
		return false;
	}

	document.forms[0].action = "create_group.cgi";
}

function on_modify_click(error_msg)
{
	document.forms[0].submitbutton.value = "modify";
	if(document.forms[0].groupname.value == "")
	{
		alert(error_msg);
		return false;
	}

	document.forms[0].action = "modify_group.cgi";
}

function on_cancel_click()
{
	document.forms[0].submitbutton.value = "cancle";
	document.forms[0].action = "index.cgi";
}

function on_submit(error_msg)
{
	document.forms[0].submit1.disabled = true;
	document.forms[0].submitcancle.disabled = true;

	if(document.forms[0].submitbutton.value == "modify")
	{
		return on_modify_click(error_msg);
	}
	if(document.forms[0].submitbutton.value == "create")
	{
		return on_create_click(error_msg);
	}
	return on_cancel_click();
}

</script>
EOF
&footer("/groups/", $text{'index'});
